package com.inossem.warehousestorage_inossem.adapter.refundStorage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStorageSelectBatchActivity;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageBatchListBean;
import com.inossem.warehousestorage_inossem.utils.RefundStorageUtils;
import com.zhy.android.percent.support.PercentLinearLayout;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class RefundStorageBatchListAdapter extends BaseAdapter {

    private Context mContext;
    private List<RefundStorageBatchListBean.DataBean.BatchListBean> mRefundStorageBatchList;
    private RefundStorageSelectBatchActivity.RefundStorageSelectBatchListener mRefundStorageSelectBatchListener;
    private LayoutInflater inflater;

    public RefundStorageBatchListAdapter(Context context, List<RefundStorageBatchListBean.DataBean.BatchListBean> refundStorageBatchList, RefundStorageSelectBatchActivity.RefundStorageSelectBatchListener refundStorageSelectBatchListener) {
        mContext = context;
        mRefundStorageBatchList = refundStorageBatchList;
        mRefundStorageSelectBatchListener = refundStorageSelectBatchListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mRefundStorageBatchList.size();
    }

    @Override
    public Object getItem(int position) {
        return mRefundStorageBatchList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RefundStorageBatchListBean.DataBean.BatchListBean batchItem = mRefundStorageBatchList.get(position);
        ViewHolder viewHolder;
        if (convertView == null || (convertView != null && convertView.getTag() == null)) {
            convertView = inflater.inflate(R.layout.view_refund_storage_batch_list, null);
            viewHolder = new ViewHolder();
            //初始化控件
            initView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //初始化数据
        initData(mContext, viewHolder, convertView, batchItem);
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.contractCode = (TextView) view.findViewById(R.id.contractCode);
        viewHolder.supplier = (TextView) view.findViewById(R.id.supplier);
        viewHolder.inStorageDate = (TextView) view.findViewById(R.id.inStorageDate);
        viewHolder.manufactureDate = (TextView) view.findViewById(R.id.manufactureDate);
        viewHolder.validityDate = (TextView) view.findViewById(R.id.validityDate);
        viewHolder.batchListLayout = (PercentLinearLayout) view.findViewById(R.id.batchListLayout);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, RefundStorageBatchListBean.DataBean.BatchListBean batchItem) {
        viewHolder.contractCode.setText(batchItem.getContractCode());
        viewHolder.supplier.setText(batchItem.getSupplier());
        viewHolder.inStorageDate.setText(batchItem.getInStorageDate());
        viewHolder.manufactureDate.setText(batchItem.getManufactureDate());
        viewHolder.validityDate.setText(batchItem.getValidityDate());
        List<RefundStorageBatchListBean.DataBean.BatchListBean.BatchInfoListBean> batchInfoList = batchItem.getBatchInfoList();
        viewHolder.batchListLayout.removeAllViews();
        if (batchInfoList != null && !batchInfoList.isEmpty()) {
            for (int i = 0; i < batchInfoList.size(); i++) {
                RefundStorageBatchListBean.DataBean.BatchListBean.BatchInfoListBean batchInfo = batchInfoList.get(i);
                RefundStorageUtils.createTextView(context, viewHolder.batchListLayout, batchInfo.getName(), batchInfo.getText());
            }
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                mRefundStorageSelectBatchListener.select(batchItem);
            }
        });
    }

    private class ViewHolder {
        private TextView contractCode, supplier, inStorageDate, manufactureDate, validityDate;

        private PercentLinearLayout batchListLayout;
    }
}
